export const desensitization = (
  value: string,
  start: number,
  end: number,
  howManyStars: number,
) => {
  let result = value.substring(0, start);

  for (let i = 0; i < howManyStars; i += 1) {
    result += '*';
  }
  result += value.substring(value.length - end, value.length);
  return result;
};
export const convertBase64UrlToBlob = (urlData) => {
  // 去掉url的头，并转换为byte
  const bytes = window.atob(urlData.split(',')[1]);
  // 处理异常,将ascii码小于0的转换为大于0
  const ab = new ArrayBuffer(bytes.length);
  const ia = new Uint8Array(ab);
  ia.forEach((i, index) => {
    ia[index] = bytes.charCodeAt(index);
  });
  return new Blob([ia], { type: urlData.split(',')[0].split(':')[1].split(';')[0] });
};
export const idCard2User = (idCardNo: string) => {
  if (!idCardNo) {
    return {};
  }
  const birth = `${idCardNo.substring(6, 10)}-${idCardNo.substring(10, 12)}-${idCardNo.substring(
    12,
    14,
  )}`;
  const gender = parseInt(idCardNo.substring(16, 1), 10) % 2 === 1 ? '男' : '女';
  const myDate = new Date();
  const month = myDate.getMonth() + 1;
  const day = myDate.getDate();
  let age = myDate.getFullYear() - parseInt(idCardNo.substring(6, 10), 10) - 1;
  if (
    parseInt(idCardNo.substring(10, 12), 10) < month ||
    (parseInt(idCardNo.substring(10, 12), 10) == month &&
      parseInt(idCardNo.substring(12, 14), 10) <= day)
  ) {
    age += 1;
  }
  return {
    birth,
    gender,
    age,
  };
};
